package fr.ing4.Macri.MonteCarlo.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.ing4.Macri.MonteCarlo.SingleThread.*;
import fr.ing4.Macri.MonteCarlo.MultiThread.*;

/**
 * 
 * @author antoinerichard
 *Custom Listener class implementing all the listeners interfaces needed
 */
public class TheListener implements ActionListener, ListSelectionListener, ChangeListener{
	private MyFrame parent;
	private int datachunk;
	
	public TheListener(MyFrame from)
	{
		this.parent=from;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		

		if(e.getSource()==parent.getGoButton())
		{
			parent.getGoButton().setText("Launched!");
			parent.getGoButton().setEnabled(false);
			
			String mode;
			double stockPr,strikePr,discountRate,maturity,carryRate,volatility,nSteps,nSimulations;
			
			if(parent.modeSl.getValue()<=1) mode="c";
			else mode="p";
			
			stockPr=parent.StkPriceSl.getValue();
			strikePr=parent.StrikePriceSl.getValue();
			discountRate=((double)parent.DiscountSl.getValue())/100;
			maturity=((double)parent.maturitySl.getValue())/12;
			carryRate=((double)parent.carrySl.getValue())/100;
			volatility=((double)parent.volaSl.getValue())/100;
			nSteps=parent.nStepsSl.getValue();
			nSimulations=parent.nSimuSl.getValue();
			datachunk=parent.chunkSl.getValue();
			
		   parent.premiumRes.setText("WORKING");
		   if(parent.multi==0){
			   parent.calculator.setValues(mode, stockPr, strikePr, maturity, discountRate, carryRate, volatility, (int)nSteps, (int)nSimulations);
		   Thread tc = new Thread((Runnable) parent.calculator);
		   tc.start();
		   }
		   else
			   {
			   parent.multilator.setValues(mode, stockPr, strikePr, maturity, discountRate, carryRate, volatility, (int)nSteps, (int)nSimulations, datachunk);
		   Thread tc = new Thread((Runnable) parent.multilator);
		   tc.start();
			   }
		}
		
		if(e.getSource().equals(parent.singleModeBtn))
			{
			parent.multi=0;
			parent.buildGUI();
			}
		if(e.getSource().equals(parent.multiModeBtn)) 
			{parent.multi=1;
			parent.buildGUI();
			}
		if(e.getSource().equals(parent.switchBtn)) 
		{
			if(parent.multi==0) parent.multi=1;
			else parent.multi=0;
			parent.buildGUI();
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		ListSelectionModel lsm=(ListSelectionModel)e.getSource();
		
		if(!parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 0).equals("Custom"))
		{
		parent.launchCustom=0;
		if(parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 0).equals("c")) parent.modeSl.setValue(0);
		if(parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 0).equals("p")) parent.modeSl.setValue(2);	
		parent.StkPriceSl.setValue(Integer.parseInt((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 1)));
		parent.StrikePriceSl.setValue(Integer.parseInt((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 2)));
		parent.DiscountSl.setValue((int) (100*Float.parseFloat(((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 3)))));
		parent.maturitySl.setValue((int) (12*Float.parseFloat(((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 4)))));
		parent.carrySl.setValue((int) (100*Float.parseFloat(((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 5)))));
		parent.volaSl.setValue((int) (100*Float.parseFloat(((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 6)))));
		parent.nStepsSl.setValue(Integer.parseInt((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 7)));
		parent.nSimuSl.setValue(Integer.parseInt((String) parent.getTable().getValueAt(lsm.getMinSelectionIndex(), 8)));
		parent.chunkSl.setValue(Runtime.getRuntime().availableProcessors());
		parent.launchCustom=1;
	}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		
		if(parent.launchCustom==1)
		{
		parent.getTable().setRowSelectionInterval(parent.getTable().getRowCount()-1, parent.getTable().getRowCount()-1);
		}
	}

}
